package com.quanquan.store.repository.marketActivity.collectCards;

import com.quanquan.store.entity.marketActivity.collectCards.CollectCardsGift;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CollectCardsGiftRepository
 * @Description
 * @Author Owen
 * @Date 2019/9/20 0020 下午 2:21
 * @Version V1.0
 **/
public interface CollectCardsGiftRepository extends JpaRepository<CollectCardsGift,Long> {

    List<CollectCardsGift> findAllByActivityIdAndStatusAndStartTimeBeforeAndEndTimeAfterAndRemainTotalGreaterThan(Long activityId, Integer status, Date startTime, Date endTime, int remianTotal);

    @Transactional
    @Modifying
    @Query(value = "update oo_collect_cards_gift set remain_total = remain_total - 1 where id = ?1 and remain_total > 0",nativeQuery = true)
    Integer substractStock(Long giftId);

}
